var layout = {
    minWidth: 0,
    minHeight: 0,
    topMargin: 10,
    sideMargins: 10,
    
    init: function() {
        layout.minWidth = parseFloat($(document.body).css('min-width')) || layout.minWidth;
        layout.minHeight = parseFloat($(document.body).css('min-height')) || parseFloat($(document.body).css('height'));
        layout.topMargin = parseFloat($(document.body).css('margin-top')) || layout.topMargin;
        layout.sideMargins = ((parseFloat($(document.body).css('margin-left')) + parseFloat($(document.body).css('margin-right'))) / 2) || layout.sideMargins;
    },
    
    resize: function(width, height, animation) {
//        console.log(width + ', ' + height);
    }
    
};

function resizeLayout(animation) {
    var width = $(window).width();
    var height = $(window).height();
    if (width < layout.minWidth) width = layout.minWidth;
    if (height < layout.minHeight) height = layout.minHeight;
    width -= layout.sideMargins * 2;
    height -=  - (+$('header').height()) - (+$('footer').height()) - layout.topMargin;
    layout.resize(width, height, animation);
}

function resizeLayoutDelay(delay) {
    if (delay===undefined) delay = 100;
    if (timeDelayResizeTimeout !== null) {
        clearTimeout(timeDelayResizeTimeout);
    }
    timeDelayResizeTimeout = setTimeout(function() {
        resizeLayout(true);
    }, delay);
}

var pjaxAfterLoadingCallback = function() {
    resizeLayout();
};

var timeDelayResizeTimeout = null;

$(function(){
    layout.init();
    window.onresize = function() {
        resizeLayoutDelay();
    };
    resizeLayout();
});